package com.boc.ewr.business.controller;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.boc.ewr.bcimp.entity.CusmTagList;
import com.boc.ewr.business.entity.AcctClassCFdbk;
import com.boc.ewr.business.entity.vo.AcctClassCFdbkVo;
import com.boc.ewr.business.service.ICusmTagRelationService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import com.boc.ewr.business.entity.AcctClassCFdbk;
import com.boc.ewr.business.service.IAcctClassCFdbkService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: 《风险核查反馈清单》-C类账户
 * @Author: jeecg-boot
 * @Date: 2023-05-26
 * @Version: V1.0
 */
@Api(tags = "《风险核查反馈清单》-C类账户")
@RestController
@RequestMapping("/business/acctClassCFdbk")
@Slf4j
public class AcctClassCFdbkController extends JeecgController<AcctClassCFdbk, IAcctClassCFdbkService> {
    @Autowired
    private IAcctClassCFdbkService acctClassCFdbkService;
    @Autowired
    private ICusmTagRelationService cusmTagRelationService;
    @Autowired
    private ISysBaseAPI sysBaseAPI;

    /**
     * 分页列表查询
     *
     * @param acctClassCFdbk
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "《风险核查反馈清单》-C类账户-分页列表查询")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-分页列表查询", notes = "《风险核查反馈清单》-C类账户-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AcctClassCFdbkVo>> queryPageList(AcctClassCFdbk acctClassCFdbk,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                         HttpServletRequest req) {
        QueryWrapper<AcctClassCFdbk> queryWrapper = QueryGenerator.initQueryWrapper(acctClassCFdbk, req.getParameterMap());
        Set<String> orgCodeSetByLoginUser = sysBaseAPI.getOrgCodeSetByLoginUser(this.getLoginUser());
        if (orgCodeSetByLoginUser!=null) {
            queryWrapper.in("branch_no", orgCodeSetByLoginUser);
        }
        Page<AcctClassCFdbk> page = new Page<AcctClassCFdbk>(pageNo, pageSize);
        IPage<AcctClassCFdbk> pageList = acctClassCFdbkService.page(page, queryWrapper);
        IPage<AcctClassCFdbkVo> convertIPage = this.convertBusInfoPage(pageList);
        return Result.OK(convertIPage);
    }

    /**
     * 添加
     *
     * @param acctClassCFdbk
     * @return
     */
    @AutoLog(value = "《风险核查反馈清单》-C类账户-添加")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-添加", notes = "《风险核查反馈清单》-C类账户-添加")
    //@RequiresPermissions("com.boc.ewr:acct_class_c_fdbk:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AcctClassCFdbk acctClassCFdbk) {
        acctClassCFdbkService.save(acctClassCFdbk);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param acctClassCFdbk
     * @return
     */
    @AutoLog(value = "《风险核查反馈清单》-C类账户-编辑")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-编辑", notes = "《风险核查反馈清单》-C类账户-编辑")
    //@RequiresPermissions("com.boc.ewr:acct_class_c_fdbk:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AcctClassCFdbk acctClassCFdbk) {
        acctClassCFdbkService.updateById(acctClassCFdbk);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "《风险核查反馈清单》-C类账户-通过id删除")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-通过id删除", notes = "《风险核查反馈清单》-C类账户-通过id删除")
    //@RequiresPermissions("com.boc.ewr:acct_class_c_fdbk:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        acctClassCFdbkService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "《风险核查反馈清单》-C类账户-批量删除")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-批量删除", notes = "《风险核查反馈清单》-C类账户-批量删除")
    //@RequiresPermissions("com.boc.ewr:acct_class_c_fdbk:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.acctClassCFdbkService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "《风险核查反馈清单》-C类账户-通过id查询")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-通过id查询", notes = "《风险核查反馈清单》-C类账户-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AcctClassCFdbk> queryById(@RequestParam(name = "id", required = true) String id) {
        AcctClassCFdbk acctClassCFdbk = acctClassCFdbkService.getById(id);
        if (acctClassCFdbk == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(acctClassCFdbk);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param acctClassCFdbk
     */
    //@RequiresPermissions("com.boc.ewr:acct_class_c_fdbk:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AcctClassCFdbk acctClassCFdbk) {
        return super.exportXls(request, acctClassCFdbk, AcctClassCFdbk.class, "《风险核查反馈清单》-C类账户");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    //@RequiresPermissions("acct_class_c_fdbk:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AcctClassCFdbk.class);
    }


    /**
     * 生成数据（将C表数据汇总到该表做导出）
     *
     * @return
     */
    //@AutoLog(value = "《风险核查反馈清单》-C类账户-生成数据")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-生成数据", notes = "《风险核查反馈清单》-C类账户-生成数据")
    //@RequiresPermissions("com.boc.ewr:acct_class_c_fdbk:createData")
    @GetMapping(value = "/createData")
    public Result<String> createData(@RequestParam(name = "caMon", required = true) String caMon,
                                     @RequestParam(name = "caPeriodNo", required = true) String caPeriodNo,
                                     @RequestParam(name = "startTime", required = true) String startTime,
                                     @RequestParam(name = "endTime", required = true) String endTime) {
        Date startTimeDate = null;
        Date endTimeDate = null;
        if (!"".equals(startTime)) {
            startTimeDate = java.sql.Date.valueOf(LocalDate.parse(startTime));
        }
        if (!"".equals(endTime)) {
            endTimeDate = java.sql.Date.valueOf(LocalDate.parse(endTime));
        }
        acctClassCFdbkService.createData(caMon, caPeriodNo, startTimeDate, endTimeDate);
        return Result.OK("数据生成成功！");
    }

    //@AutoLog(value = "《风险核查反馈清单》-C类账户-通过cusmNm查询标签")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-通过cusmNm查询标签", notes = "《风险核查反馈清单》-C类账户-通过cusmNm查询标签")
    @GetMapping(value = "/queryTageByCusmNm")
    public Result<Map<String, List<CusmTagList>>> queryTageByCusmNm(@RequestParam(name = "cusmNm", required = true) String cusmNm) {

        Map<String, List<CusmTagList>> map = cusmTagRelationService.queryTageByCusmNm(cusmNm);
        if (map.size() == 0) {
            return Result.error("未根据客户名称查询到客户信息！");
        } else {
            return Result.OK(map);
        }
    }

    //@AutoLog(value = "《风险核查反馈清单》-C类账户-通过CusmNm刷新标签")
    @ApiOperation(value = "《风险核查反馈清单》-C类账户-通过CusmNm刷新标签", notes = "《风险核查反馈清单》-C类账户-通过CusmNm刷新标签")
    @GetMapping(value = "/updateTagByCusmNm")
    public Result<String> updateTagByCusmNm(@RequestParam(name = "cusmNm", required = true) String cusmNm) {

        cusmTagRelationService.updateTagCusmNm(cusmNm);

        return Result.OK("标签刷新成功");
    }

    /**
     * 将 Page转出成Vo的Page
     */
    public IPage<AcctClassCFdbkVo> convertBusInfoPage(IPage<AcctClassCFdbk> page) {
        List<AcctClassCFdbkVo> voList = page.getRecords().stream()
                .map(this::convertAcctClass)
                .collect(Collectors.toList());

        IPage<AcctClassCFdbkVo> voPage = new Page<>();
        voPage.setCurrent(page.getCurrent());
        voPage.setSize(page.getSize());
        voPage.setTotal(page.getTotal());
        voPage.setRecords(voList);

        return voPage;
    }

    private AcctClassCFdbkVo convertAcctClass(AcctClassCFdbk entity) {
        AcctClassCFdbkVo vo = new AcctClassCFdbkVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    private LoginUser getLoginUser() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser;
    }
}
